//
//  PointPopView.m
//  markmap
//
//  Created by 甘世清 on 2024/3/24.
//

#import "PointPopView.h"
#import "MapDetailViewController.h"
@implementation PointPopView

- (void)SQ_UIViewForSelfView{
    self.backgroundColor = SQColor(@"#4A4A4A");
    self.layer.cornerRadius = 48;
    self.layer.masksToBounds = YES;
 
   
    [self addSubview:self.avatarImageView];
    [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(12);
        make.top.equalTo(self).offset(30);
        make.size.mas_equalTo(40);
    }];
    [self addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(62);
        make.top.equalTo(self.avatarImageView).offset(4);
    }];
    [self addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(62);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(2);
    }];
    
    [self addSubview:self.likeBtn];
    [self.likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-14);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(44);
        make.centerY.equalTo(self.avatarImageView);
    }];
    WS(weakSelf);
    [self.likeBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_isLikeButtonClick];
    }];
    [self addSubview:self.topLabel];
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(12);
        make.top.equalTo(self.avatarImageView.mas_bottom).offset(10);
    }];
    [self addSubview:self.messageLabel];
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(12);
        make.top.equalTo(self.topLabel.mas_bottom).offset(10);
        make.right.equalTo(self.mas_right).offset(-12);
    }];

    [self addSubview:self.addButton];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDETH-110);
        make.centerX.equalTo(self);
        make.bottom.equalTo(self.mas_bottom).offset(-20-FootHeight);
        make.height.mas_equalTo(48);
    }];
   
}
-(void)SQ_dataForId:(NSString*)userId{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
     [dic setObject:userId forKey:@"id"];
     [HttpTool post:Url8 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
         self.model = [PointModel mj_objectWithKeyValues:httpModel.data];
     } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
         [YJProgressHUD showMsgWithoutView:httpModel.message];
     }];
}

- (void)setModel:(PointModel *)model{
    _model = model;
    [self.avatarImageView.headerImage sd_setImageWithURL:SQURLImage(model.portrait)];
    self.nameLabel.text = model.name;
    self.topLabel.text = model.title;
    self.messageLabel.text = model.remark;
    self.timeLabel.text = model.createTime;
    if (model.isLike) {
        self.likeBtn.image = SQGetImage(@"yidianzan");
    } else {
        self.likeBtn.image = SQGetImage(@"arrow");
    }
}
-(void)SQ_addAnntionForMap{
    MapDetailViewController * vc = [[MapDetailViewController alloc]init];
    vc.model = self.model;
    [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
    WS(weakSelf);
    [vc setIsLikeBtnBlock:^(PointModel * _Nonnull model) {
        if (model.isLike) {
            weakSelf.likeBtn.image = SQGetImage(@"yidianzan");
        } else {
            weakSelf.likeBtn.image = SQGetImage(@"arrow");
        }
    }];
}
-(void)SQ_isLikeButtonClick{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setValue:self.model.userid forKey:@"pid"];
    if (self.model.isLike) {
        [dic setValue:@"0" forKey:@"type"];
    } else {
        [dic setValue:@"1" forKey:@"type"];
    }
    [HttpTool post:Url13 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.model.isLike = !self.model.isLike;
        if (self.model.isLike) {
            self.likeBtn.image = SQGetImage(@"yidianzan");
            self.model.likeNum ++;
        } else {
            self.likeBtn.image = SQGetImage(@"arrow");
            self.model.likeNum --;
        }
        if (self.isLikeBtnBlock) {
            self.isLikeBtnBlock(self.model);
        }  
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
-(void)SQ_PushPeople{
    PeopleDetailViewController * vc = [[PeopleDetailViewController alloc]init];
    vc.uid = self.model.uid;
    [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
}
- (HeaderView *)avatarImageView{
    if (!_avatarImageView) {
        _avatarImageView = [[HeaderView alloc]init];
        _avatarImageView.clipsToBounds = YES;
        _avatarImageView.layer.cornerRadius = 20;
        _avatarImageView.layer.masksToBounds = YES;
        _avatarImageView.headerImage.contentMode = UIViewContentModeScaleAspectFill;
        [_avatarImageView.btn addTarget:self action:@selector(SQ_PushPeople) forControlEvents:UIControlEventTouchUpInside];
    }
    return _avatarImageView;
}
- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.backgroundColor = UIColor.clearColor;
        _nameLabel.font = FONT_REGULAR(14);
        _nameLabel.textColor = UIColor.whiteColor;
    }
    return _nameLabel;
}
- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.backgroundColor = UIColor.clearColor;
        _timeLabel.font = FONT_REGULAR(12);
        _timeLabel.textColor = SQColor(@"#909090");
    }
    return _timeLabel;
}
- (UILabel *)messageLabel{
    if (!_messageLabel) {
        _messageLabel = [[UILabel alloc]init];
        _messageLabel.font = FONT_REGULAR(14);
        _messageLabel.textColor = SQColor(@"#909090");
        _messageLabel.backgroundColor = UIColor.clearColor;
        _messageLabel.numberOfLines = 0;
    }
    return _messageLabel;
}

- (UILabel *)topLabel{
    if (!_topLabel) {
        _topLabel = [[UILabel alloc]init];
        _topLabel.font = FONT_MEDIUM(16);
        _topLabel.textColor = UIColor.whiteColor;
        _topLabel.backgroundColor = UIColor.clearColor;
        _topLabel.numberOfLines = 0;
    }
    return _topLabel;
}

 
- (SQCustomButton *)likeBtn{
    if (!_likeBtn) {
        _likeBtn = [[SQCustomButton alloc]initType:SQCustomButtonRightImageType imageSize:CGSizeMake(34, 34) midmargin:0];
        _likeBtn.image = SQGetImage(@"arrow");
        _likeBtn.backgroundColor = UIColor.clearColor;
    }
    return _likeBtn;
}

- (HeaderView *)addButton{
    if (!_addButton) {
        _addButton = [[HeaderView alloc]init];
        _addButton.image = @"矩形";
        _addButton.colorString = @"#FFFFFF";
        [_addButton.btn addTarget:self action:@selector(SQ_addAnntionForMap) forControlEvents:UIControlEventTouchUpInside];
        _addButton.layer.cornerRadius = 6;
        _addButton.layer.masksToBounds = YES;
        _addButton.text = SQNSLocalString(@"查看详情");
        _addButton.font = FONT_MEDIUM(18);
    }
    return _addButton;
}
 
@end
